package com.chexiehui.jc.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aries.ui.view.radius.RadiusTextView;
import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpActivity;
import com.chexiehui.jc.entity.BrandInfo;
import com.chexiehui.jc.entity.BrandSeriesInfo;
import com.chexiehui.jc.entity.CarSeriesInfo;
import com.chexiehui.jc.entity.DictInfo;
import com.chexiehui.jc.entity.SpecialBrandSeriesGroupInfo;
import com.chexiehui.jc.entity.SpecialCarSeriesInfo;
import com.chexiehui.jc.mvp.car.SpecialCarSeriesGroupCovenant;
import com.chexiehui.jc.mvp.car.SpecialCarSeriesGroupPresenter;
import com.chexiehui.jc.utils.BundleBuilder;
import com.chexiehui.jc.utils.GlideUtil;
import com.chexiehui.jc.utils.NoDoubleClickUtils;
import com.chexiehui.jc.widget.LoadingLayout;
import com.chexiehui.jc.widget.indexbar.suspension.SuspensionDecoration;
import com.nex3z.flowlayout.FlowLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 新车特卖实时数量
 */
public class SpecialCarSeriesGroupActivity extends BaseMvpActivity<SpecialCarSeriesGroupPresenter> implements SpecialCarSeriesGroupCovenant.View{

    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.loadingLayout)
    LoadingLayout loadingLayout;

    private BrandInfo brandInfo;
    private List<SpecialCarSeriesInfo> saleListData;
    private CommonAdapter<SpecialCarSeriesInfo> saleListAdapter;
    private SuspensionDecoration suspensionDecoration;

    @Override
    protected void beforeSetView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            brandInfo = (BrandInfo) bundle.getSerializable("entity");
        }
        if (brandInfo == null) {
            brandInfo = new BrandInfo();
        }
    }

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText(brandInfo.getName());
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_special_car_series_group;
    }

    @Override
    protected void initView(Bundle var1) {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableOverScrollDrag(true);
        refreshLayout.setDragRate(0.5f);
        loadingLayout.setStatus(LoadingLayout.Loading);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        saleListData = new ArrayList<>();
        saleListAdapter = new CommonAdapter<SpecialCarSeriesInfo>(mContext, R.layout.item_special_car_series_group, saleListData) {
            @Override
            protected void convert(ViewHolder holder, SpecialCarSeriesInfo item, int position) {
                holder.setText(R.id.tv_item_name, item.getName());
                holder.setText(R.id.tv_item_count, item.getCount() + "");
            }
        };
        saleListAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (NoDoubleClickUtils.isDoubleClick()) return;
                startActivity(SpecialCarListActivity.class,
                        new BundleBuilder().putString("seriesId", saleListData.get(position).getId())
                                .create());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mRecycler.setAdapter(saleListAdapter);
        suspensionDecoration = new SuspensionDecoration(mContext, saleListData);
        mRecycler.addItemDecoration(suspensionDecoration);
        loadingLayout.setOnReloadListener(v -> {
            if (brandInfo != null) {
                loadingLayout.setStatus(LoadingLayout.Loading);
                mvpPresenter.getList();
            } else {
                loadingLayout.setStatus(LoadingLayout.Empty);
                loadingLayout.setEmptyText("没有找到任何数据");
            }
        });
        if (brandInfo == null || StringUtils.isTrimEmpty(brandInfo.getId())) {
            loadingLayout.setStatus(LoadingLayout.Empty);
            loadingLayout.setEmptyText("没有找到任何数据");
            return;
        }
        mvpPresenter.getList();
    }

    @Override
    protected SpecialCarSeriesGroupPresenter createPresenter() {
        return new SpecialCarSeriesGroupPresenter(this);
    }

    @Override
    public void onGetListSuccess(BaseModel<List<SpecialBrandSeriesGroupInfo>> successResult) {
        if (loadingLayout.getStatus() != LoadingLayout.Success) {
            loadingLayout.setStatus(LoadingLayout.Success);
        }
        saleListData.clear();
        if (successResult.getData().size() <= 0) {
            loadingLayout.setStatus(LoadingLayout.Empty);
            loadingLayout.setEmptyText("没有找到任何数据");
        } else {
            for (int i = 0; i < successResult.getData().size(); i++) {
                for (int j = 0; j < successResult.getData().get(i).getSeriesList().size(); j++) {
                    successResult.getData().get(i).getSeriesList().get(j)
                            .setTypeName(successResult.getData().get(i).getTypeName());
                }
                saleListData.addAll(successResult.getData().get(i).getSeriesList());
            }
        }
        saleListAdapter.notifyDataSetChanged();
        suspensionDecoration.setmDatas(saleListData);
    }

    @Override
    public void onGetListFailure(BaseModel<Object> failureResult) {
        if (Constants.PAGE_DATA_EMPTY == failureResult.getResultCode()) {
            loadingLayout.setStatus(LoadingLayout.Empty);
            loadingLayout.setEmptyText(failureResult.getResultInfo());
        } else {
            loadingLayout.setStatus(LoadingLayout.Error);
            loadingLayout.setErrorText(failureResult.getResultInfo());
        }
    }

    @Override
    public String getBrandId() {
        return brandInfo.getId();
    }

}
